package com.montran.timer.task;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Timer;
import java.util.TimerTask;

public class ManagerScheduleTask {
	// expressed in milliseconds
	private final static long fONCE_PER_DAY = 1000 * 60 * 60 * 24;
	
	public void scheduleFutureTask(){
		 TimerTask fetchMail  = new FutureTask();

		    //perform the task once a day at 4 a.m., starting tomorrow morning
		    //(other styles are possible as well)
		    Timer timer = new Timer();
		    timer.scheduleAtFixedRate(fetchMail, getScheduleDate(1,4,0), fONCE_PER_DAY);
	}
	
	private static Date getScheduleDate(Integer delayDays, Integer hour, Integer minute){
	    Calendar tomorrow = new GregorianCalendar();
	    tomorrow.add(Calendar.DATE, delayDays);
	    Calendar result = new GregorianCalendar(
	      tomorrow.get(Calendar.YEAR),
	      tomorrow.get(Calendar.MONTH),
	      tomorrow.get(Calendar.DATE),
	      hour,
	      minute
	    );
	    return result.getTime();
	  }
}
